﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Dos.ORM;
using Finance.Entity;
using Finance.IDal;
using Finance.Poco;

namespace Finance.Dal
{
    public class BankCardDal : BaseDal, IBankCardDal
    {
        public IEnumerable<Entity.BankCardEntity> List(string cardNo, int pageIndex, int pageSize, out int total)
        {
            var from = dbSession.From<BankCard>();
            if (!string.IsNullOrWhiteSpace(cardNo))
            {
               from.Where(card => card.CardNo.Contains(cardNo));
            }

            total = from.Count();

            var bankCardList = from.Page(pageSize, pageIndex).ToList();

            return bankCardList.Select(bc => new BankCardEntity
                {
                    Id = bc.Id,
                    CardNo = bc.CardNo,
                    Name = bc.Name,
                    BankName = bc.BankName,
                    Amount = bc.Amount,
                }).ToList();

        }

        public bool Save(Entity.BankCardEntity entity)
        {
            if (entity != null)
            {
                BankCard bankCard = new BankCard();
                bankCard.Id = entity.Id;
                bankCard.CardNo = entity.CardNo;
                bankCard.Name = entity.Name;
                bankCard.BankName = entity.BankName;
                bankCard.Amount = entity.Amount;


                // 获取银行名称
                //var cardNo = bankCard.CardNo;
                //var bankCardBin =
                //    dbSession.From<BankCardBin>().Where(bcb=>cardNo.Like( bcb.CardBin)).First();
                var bankCardBin =
                    dbSession.FromSql("select * from bank_card_bin where :cardNo like concat(cardBin,'%') ")
                             .AddInParameter("cardNo", DbType.String, bankCard.CardNo)
                             .ToFirst<BankCardBin>();
                if (bankCardBin != null)
                {
                    bankCard.BankName = bankCardBin.BankName;
                }
                else
                {
                    bankCard.BankName = null;
                }

                if (bankCard.Id == 0)
                {
                    bankCard.Attach(EntityState.Added);
                }
                else
                {
                    bankCard.Attach(EntityState.Modified);
                }
                return dbSession.Save(bankCard) > 0;
            }
            return false;
        }

        public bool Delete(int id)
        {
            return dbSession.Delete<BankCard>(id) > 0;
        }


        public object List()
        {
            var from = dbSession.From<BankCard>();
           

            var bankCardList = from.ToList();

            return bankCardList.Select(bc => new BankCardEntity
            {
                Id = bc.Id,
                CardNo = bc.CardNo,
                Name = bc.Name,
                BankName = bc.BankName,
                Amount = bc.Amount,
            }).ToList();
        }
    }
}
